#pragma once

#include "nocopy.hpp"
#include "log.hpp"
#include <sys/epoll.h>
#include <unistd.h>

static const int size = 128;
static const int timeout = 3000;

class Epoller : public Nocopy  //不让Epoller进行拷贝和赋值运算符重载操作
{
public:
    Epoller()
    {
        _epfd = epoll_create(size);
        if(_epfd == -1)
        {
            LOG(Fatal, "epoll_create fail");
        }
        LOG(Info, "epoll_create success");
    }
    //参数：用来获取就绪队列中的节点事件，一次最多获取多少个
    int EpollerWait(struct epoll_event* revents, int num)
    {
        int n = epoll_wait(_epfd, revents, num, timeout);
        return n;
    }
    //参数：做什么操作， 对哪个文件描述符做， 做什么事件
    bool EpollerUpdate(int oper, int sockfd, uint32_t event)
    {
        int n = 0;
        if(oper == EPOLL_CTL_DEL)
        {
            n = epoll_ctl(_epfd, oper, sockfd, nullptr);
        }
        else  //oper == EPOLL_CTL_ADD || oper == EPOLL_CTL_MOD
        {
            struct epoll_event ev;
            ev.events = event;
            ev.data.fd = sockfd;

            n = epoll_ctl(_epfd, oper, sockfd, &ev);
        }
        if(n == -1) return false;
        std::cout << "ADD success" << sockfd << std::endl;
        return true;
    }
    ~Epoller()
    {
        if(_epfd >= 0)
            close(_epfd);
    }
private:
    int _epfd;
};